<template>
  <div class="table-container">
    <div class="table-breadcrumb">
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>用户管理</el-breadcrumb-item>
        <el-breadcrumb-item>积分列表</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
<!--    <div class="table-search">-->
<!--      <el-row>-->
<!--        <el-input-->
<!--          style="width: 200px;margin-right: 10px"-->
<!--          placeholder="请输入优惠券信息查询"-->
<!--          v-model="keyword"-->
<!--          clearable-->
<!--          @clear="queryList"-->
<!--        >-->
<!--        </el-input>-->
<!--        <el-button type="primary" @click="queryList" >搜索</el-button>-->
<!--      </el-row>-->
<!--    </div>-->
    <div class="table-content">
      <div class="table-bar">
        <el-button type="primary" @click="handleAdd">新增积分</el-button>
      </div>
      <el-table
        border
        empty-text="当前数据空空如也"
        ref="multipleTable"
        :data="userLevels"
        tooltip-effect="dark"
        style="width: 100%"
      >
        <el-table-column prop="id" label="ID" width="60"></el-table-column>
        <el-table-column prop="name" label="名称" ></el-table-column>
        <el-table-column prop="exp" label="每次获得经验值" width="auto"></el-table-column>
        <el-table-column prop="exp_max" label="每日最多获得经验值次数" width="auto">
          <template slot-scope="scope">
            <p v-if="scope.row.exp_max == -1">不限</p>
            <p v-else >{{scope.row.exp_max}}</p>
          </template>
        </el-table-column>
        <el-table-column prop="exp_total" label="总最多获得经验值次数" width="auto">
          <template slot-scope="scope">
            <p v-if="scope.row.exp_total == -1">不限</p>
            <p v-else >{{scope.row.exp_total}}</p>
          </template>
        </el-table-column>
        <el-table-column prop="points" label="每次获得积分" width="auto"></el-table-column>
        <el-table-column prop="points_max" label="每日最多获得积分次数" width="auto">
          <template slot-scope="scope">
            <p v-if="scope.row.points_max == -1">不限</p>
            <p v-else >{{scope.row.points_max}}</p>
          </template>
        </el-table-column>
        <el-table-column prop="points_total" label="总最多获得积分次数" width="auto">
          <template slot-scope="scope">
            <p v-if="scope.row.points_total == -1">不限</p>
            <p v-else >{{scope.row.points_total}}</p>
          </template>
        </el-table-column>
        <el-table-column prop="not_del" label="是否可删除" width="auto">
          <template slot-scope="scope">
            <p v-if="scope.row.not_del == 0">不可删除</p>
            <p v-else >可删除</p>
          </template>
        </el-table-column>
        <el-table-column label="操作" fixed="right">
          <template slot-scope="scope">
            <el-button
              size="mini"
              style="margin-right: 5px"
              @click="handleEdit(scope.$index, scope.row)">编辑
            </el-button>
            <el-popconfirm
              v-if="scope.row.not_del != 0"
              confirm-button-text='确定'
              cancel-button-text='点错了'
              icon="el-icon-info"
              icon-color="red"
              title="这是一段内容确定删除吗？"
              @confirm="handleDelete(scope.index,scope.row)"
            >
              <el-button  slot="reference" type="danger" size="mini">删除</el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <div class="page-nav">
        <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="page"
          :page-sizes="[10, 20, 30, 50,100]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        page: 1,
        pageSize: 10,
        total: 0,
        keyword: '',
        userLevels: [],
      }
    },
    mounted() {
      this.queryList();
    },
    methods: {
      handleAdd() {
        this.$router.push({name: 'userPointAdd'})
      },
      handleEdit(index, row) {
        const id = row.id;
        this.$router.push({name: 'userPointEdit', query: {id}});
      },
      handleDelete(index, row) {
        this.toDelete(row.id);
      },

      handleSizeChange(val) {
        this.pageSize = val;
        this.page = 1;
        this.queryList()
      },
      handleCurrentChange(val) {
        this.page = val;
        this.queryList();
      },
      queryList() {
        this.$http.get('/api/root/UserPoints/pointsList', {
          page: this.page,
          pagesize: this.pageSize,
          keyword: this.keyword
        }).then(rest => {
          if (rest.code == 1) {
            this.userLevels = rest.data.list;
            this.total = rest.data.total;
          }
        })
      },
      toDelete(id_lst) {
        this.$http.get('/api/root/UserPoints/pointsDel', {
          id: id_lst,
        }).then(rest => {
          if (rest.code == 1) {
            this.queryList();
            this.$message.success('删除成功');
          } else {
            this.$message.error("删除失败");
          }
        })
      }
    }
  }
</script>
<style lang="less" scoped>
  @import 'index.less';
</style>
